﻿using System;
using System.Collections.Generic;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine.UI;
using SmlEngine.UI.Managers;
using SmlEngine.UI.Screens;
using SmlEngine.Sprites.Collections;
using Sml.Scenes;

namespace Sml.Screens
{
    /// <summary>
    /// Encapsulates and manages a set of WorldScenes.
    /// </summary>
    public class WorldScreen : Screen
    {
        private WorldScene w;
        private List<string> file_Paths;
        private Vector2 screenSize;

        private bool IsLoaded
        {
            get
            {
                foreach (WorldScene s in Scenes)
                {
                    foreach (SmlEngine.Sprites.Collections.World w in s.Worlds)
                    {
                        if (!w.IsLoaded) return false;
                    }
                }
                return true;
            }
        }

        public override IEnumerable<string> PreloadedAssets
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Constructs a new WorldScreen instance.
        /// </summary>
        /// <param name="filePaths">The file paths to the worlds to use in this screen.</param>
        /// <param name="c_screenSize">The size of the window or screen.</param>
        public WorldScreen(List<string> filePaths, Vector2 c_screenSize)
            : base()
        {
            TransitionOnTime = TimeSpan.FromSeconds(2.5);
            TransitionOffTime = TimeSpan.FromSeconds(2.5);
            file_Paths = filePaths;
            screenSize = c_screenSize;
        }

        /// <summary>
        /// Initializes this WorldScreen.
        /// </summary>
        public override void Initialize()
        {
            if (Scenes.Count == 0)
            {
                w = new WorldScene(this, file_Paths, screenSize, 0);
                Scenes.Add(w);
                w.Initialize();
            }

            base.Initialize();
        }

        /// <summary>
        /// Loads the content for this screen.
        /// </summary>
        /// <param name="content">The ContentManager to use.</param>
        public override void LoadContent(ContentManager content)
        {
            base.LoadContent(content);
            w.Load(content, ScreenManager.SpriteBatch.GraphicsDevice);
            w.LoadContent(content);
        }

        /// <summary>
        /// Unloads the content for this screen.
        /// </summary>
        public override void UnloadContent()
        {
            foreach (WorldScene scene in Scenes) scene.Worlds.ForEach(w => w.UnloadContent());
        }

        /// <summary>
        /// Handles the input for this screen.
        /// </summary>
        /// <param name="input">The InputManager to use.</param>
        public override void HandleInput(InputManager input)
        {
            base.HandleInput(input);
        }

        /// <summary>
        /// Draws this WorldScreen.
        /// </summary>
        /// <param name="gameTime">The timing information for the game.</param>
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
 //         ScreenManager.FadeBackBufferToBlack(TransitionAlpha);
        }

        /// <summary>
        /// Returns the WorldScene by index.
        /// </summary>
        /// <param name="index">The index of the WorldScene to return.</param>
        /// <returns>The WorldScene at the index.</returns>
        public WorldScene GetScene(int index)
        {
            if (!(index < 0 || index > Scenes.Count))
            {
                return Scenes[index] as WorldScene;
            }
            else return null;
        }
    }
}
